<?php
/**
 * @desc 
 * @author bestshow <blog.csdn.net/likunlun1234>
 * @time 2014-07-12 19:26:23
 */
class Captcha extends CCaptchaAction{
    //重写run方法，使得验证码在页面刷新时刷新
	public function run(){
		if (isset($_GET[self::REFRESH_GET_VAR])){
			$code = $this->getVerifyCode(true);
			echo CJSON::encode(array(
				'hash1' => $this->generateValidationHash($code),
				'hash2' => $this->generateValidationHash(strtolower($code)),
				'url' => $this->getController()->createUrl($this->getId(), array('v' => uniqid())),
			));
		}else {
			$this->renderImage($this->getVerifyCode(true));
			Yii::app()->end();
		}	
	}
	//重写validate方法，使得验证码在提交过后刷新
	public function validate($input,$caseSensitive)
	{	    
	    $code = $this->getVerifyCode();
	    $valid = $caseSensitive ? ($input === $code) : !strcasecmp($input,$code);
	    if($valid){
	        $session = Yii::app()->session;
	        $session->open();
	        $name = $this->getSessionKey();
	        $session[$name]=null;
	    }else{
	        $session = Yii::app()->session;
	        $session->open();
	        $name = $this->getSessionKey() . 'count';
	        $session[$name] = $session[$name] + 1;
	        if($session[$name] > $this->testLimit && $this->testLimit > 0)
	            $this->getVerifyCode(true);
	    }
	    return $valid;
	}
}